{extend name="../../base/view/common/base" /}
{block name="style"}
<style>
    .layui-table-view .layui-table thead th {
        background-color: #f2f2f2;
    }
</style>
{/block}
<!-- 主体 -->
{block name="body"}
<div class="layui-form p-page">
    <form class="layui-form" id="searchForm">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">关键词</label>
                <div class="layui-input-inline">
                    <input type="text" name="keywords" placeholder="请输入关键词" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <button class="layui-btn" lay-submit lay-filter="searchForm">搜索</button>
                <button type="reset" class="layui-btn layui-btn-reset" lay-filter="table-reset">清空</button>
            </div>
        </div>
    </form>
    <table id="tableList" lay-filter="tableList"></table>
</div>
{/block}

<!-- 脚本 -->
{block name="script"}
<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm tool-add" data-href="/lawcase/party/add">+ 添加当事人</button>
    </div>
</script>
<script type="text/html" id="column-toolbar">
    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="view"><i class="layui-icon layui-icon-search"></i> 查看</a>
    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit"><i class="layui-icon layui-icon-edit"></i> 编辑</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del"><i class="layui-icon layui-icon-delete"></i> 删除</a>
</script>
<script>
const moduleInit = ['tool', 'tablePlus'];
function gouguInit() {
    table = layui.tablePlus,
    form = layui.form,
    layer = layui.layer,
    tool = layui.tool,
    util = layui.util;

    // 渲染表格
    table.render({
        elem: '#tableList',
        title: '当事人列表',
        url: '/lawcase/party/index',
        toolbar: '#toolbar',
        page: {
            layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
        },
        limit: 20,
        limits: [10, 20, 30, 50],
        cellMinWidth: 80,
        height: 'full-152',
        cols: [[
            {field: 'id', title: 'ID', width: 80, sort: true, align: 'center'},
            {field: 'name', title: '当事人名称', width: 150},
            {field: 'type_text', title: '类型', width: 80, align: 'center', templet: function(d){
                return d.party_type == 1 ? '个人' : '企业';
            }},
            {field: 'id_number', title: '证件号码', width: 180, align: 'center'},
            {field: 'contact', title: '联系人', width: 120, align: 'center'},
            {field: 'phone', title: '联系电话', width: 120, align: 'center'},
            {field: 'full_address', title: '联系地址', width: 200, templet: function(d){
                return (d.province_name || '') + (d.city_name || '') + (d.district_name || '') + (d.address || '');
            }},
            {field: 'create_time', title: '创建时间', width: 180, align: 'center', 
            templet: d => util.toDateString(d.create_time * 1000, 'yyyy-MM-dd HH:mm:ss')},
            {field: 'update_time', title: '更新时间', width: 180, align: 'center', 
            templet: d => d.update_time > 0 ? util.toDateString(d.update_time * 1000, 'yyyy-MM-dd HH:mm:ss') : util.toDateString(d.create_time * 1000, 'yyyy-MM-dd HH:mm:ss')},
            {title: '操作', width: 200, align: 'center', fixed: 'right', toolbar: '#column-toolbar'}
        ]],
        page: true,
        limit: 10,
        limits: [10, 20, 30, 40, 50],
        text: {
            none: '<div class="gg-no-data-tips"><i class="layui-icon layui-icon-face-surprised"></i><p>暂无相关数据</p></div>'
        },
        parseData: function(res){
            if(typeof res === 'string') {
                try {
                    res = JSON.parse(res);
                } catch(e) {
                    return {
                        code: -1,
                        msg: '数据解析失败',
                        count: 0,
                        data: []
                    };
                }
            }
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.count,
                "data": res.data
            };
        }
    });

    // 监听工具条
    table.on('tool(tableList)', function(obj){
        var data = obj.data;
        if(obj.event === 'edit'){
            tool.side("/lawcase/party/add?id=" + data.id, '编辑当事人');
        } else if(obj.event === 'view'){
            tool.side("/lawcase/party/view?id=" + data.id, '查看当事人');
        } else if(obj.event === 'del'){
            layer.confirm('确定要删除此当事人吗？', function(index){
                tool.delete("/lawcase/party/delete", {id: data.id}, function(res){
                    if(res.code === 0) {
                        obj.del();
                        table.reload('tableList');
                        layer.msg('删除成功', {time: 1000});
                    } else {
                        layer.msg(res.msg || '删除失败', {time: 2000});
                    }
                });
                layer.close(index);
            });
        }
    });

    // 监听搜索表单提交
    form.on('submit(searchForm)', function(data){
        table.reload('tableList', {
            page: {
                curr: 1
            },
            where: data.field
        });
        return false;
    });

    // 添加当事人按钮点击事件
    $('#addParty').click(function() {
        tool.side("lawcase/party/add", '添加当事人');
    });
}
</script>

{/block}
// ... existing code ...